package light.mvc.service.demo.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.demo.DemoBO;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.demo.DemoVO;
import light.mvc.service.demo.DemoServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service                                                                                                           
public class DemoServiceImpl implements DemoServiceI {                                                                            
                                                                                                                                  
	@Autowired                                                                                                                     
	private BaseDaoI<DemoBO> demoDao;                                                                                              
                                                                                                                                  
	@Override                                                                                                                      
	public void add(DemoVO r) {                                                                                                    
		DemoBO t = new DemoBO();                                                                                                   
		t.setName(r.getName());                                                                                                    
		t.setDescription(r.getDescription());                                                                                      
		demoDao.save(t);                                                                                                           
	}                                                                                                                              
                                                                                                                                  
	@Override                                                                                                                      
	public void delete(Long id) {                                                                                                  
		DemoBO t = demoDao.get(DemoBO.class, id);                                                                                  
		demoDao.delete(t);                                                                                                         
	}                                                                                                                              
                                                                                                                                  
	@Override                                                                                                                      
	public void edit(DemoVO r) {                                                                                                   
		DemoBO t = demoDao.get(DemoBO.class, r.getId());                                                                           
		t.setDescription(r.getDescription());                                                                                      
		t.setName(r.getName());                                                                                                    
		demoDao.update(t);                                                                                                         
	}                                                                                                                              
                                                                                                                                  
	@Override                                                                                                                      
	public DemoVO get(Long id) {                                                                                                   
		DemoBO t = demoDao.get(DemoBO.class, id);                                                                                  
		DemoVO r = new DemoVO();                                                                                                   
		r.setDescription(t.getDescription());                                                                                      
		r.setId(t.getId());                                                                                                        
		r.setName(t.getName());                                                                                                    
		return r;                                                                                                                  
	}                                                                                                                              
                                                                                                                                  
	@Override                                                                                                                      
	public List<DemoVO> dataGrid(DemoVO demo, PageFilter ph) {                                                                     
		List<DemoVO> ul = new ArrayList<DemoVO>();                                                                                 
		Map<String, Object> params = new HashMap<String, Object>();                                                                
		String hql = " from DemoBO t ";                                                                                            
		List<DemoBO> l = demoDao.find(hql + whereHql(demo, params) + orderHql(ph), params, ph.getPage(), ph.getRows());            
		for (DemoBO t : l) {                                                                                                       
			DemoVO u = new DemoVO();                                                                                               
			BeanUtils.copyProperties(t, u);                                                                                        
			ul.add(u);                                                                                                             
		}                                                                                                                          
		return ul;                                                                                                                 
	}                                                                                                                              
                                                                                                                                  
	@Override                                                                                                                      
	public Long count(DemoVO demo, PageFilter ph) {                                                                                
		Map<String, Object> params = new HashMap<String, Object>();                                                                
		String hql = " from DemoBO t ";                                                                                            
		return demoDao.count("select count(*) " + hql + whereHql(demo, params), params);                                           
	}                                                                                                                              
                                                                                                                                  
	private String whereHql(DemoVO demo, Map<String, Object> params) {                                                             
		String hql = "";                                                                                                           
		if (demo != null) {                                                                                                        
			hql += " where 1=1 ";                                                                                                  
			if (demo.getName() != null) {                                                                                          
				hql += " and t.name like :name";                                                                                   
				params.put("name", "%%" + demo.getName() + "%%");                                                                  
			}                                                                                                                      
		}                                                                                                                          
		return hql;                                                                                                                
	}                                                                                                                              
                                                                                                                                  
	private String orderHql(PageFilter ph) {                                                                                       
		String orderString = "";                                                                                                   
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {                                                                   
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();                                                     
		}                                                                                                                          
		return orderString;                                                                                                        
	}                                                                                                                              
}